package ch.fhnw.qualiscan.application.views;

import java.util.Iterator;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.ProgressIndicator;
import com.vaadin.ui.Button.ClickEvent;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.controllers.EditSurvey;
import ch.fhnw.qualiscan.application.modals.DomainHelper;
import ch.fhnw.qualiscan.core.View;
import ch.fhnw.qualiscan.core.db.pojos.Domain;
import ch.fhnw.qualiscan.core.db.pojos.DomainText;
import ch.fhnw.qualiscan.core.db.pojos.Language;
import ch.fhnw.qualiscan.core.db.pojos.Question;
import eu.livotov.tpt.i18n.TM;

@SuppressWarnings("serial")
public class EditSurveyView extends View{
	
	private EditSurvey controller;
	private Panel panel2;
	
	public EditSurveyView(EditSurvey controller) {
		this.controller = controller;
	}

	@Override
	public void initComponents() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void buildLayout() {
		addComponent(buildPanel2());
		
	}
	
public Panel buildPanel2() {
		
		panel2 = new Panel();
		panel2.addComponent(textHelper.h2(TM.get("editsurvey.tab1.ownprogresspanel.title")));
		
		Button domainHelp = new Button(TM.get("helper.domainhelp"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				openModal(new DomainHelper());
			}
		});
		domainHelp.setStyleName(Button.STYLE_LINK);
		
		CssLayout spacer = new CssLayout();
		spacer.setHeight("15px");
		spacer.setWidth("100%");
                CssLayout spacer2 = new CssLayout();
		spacer2.setHeight("15px");
		spacer2.setWidth("100%");
                
		panel2.addComponent(spacer);
		panel2.addComponent(domainHelp);
		panel2.addComponent(spacer2);
                
		boolean isSurveyFinished = true;
		CssLayout surveyFinishedWrap = new CssLayout();
		surveyFinishedWrap.setVisible(false);
		surveyFinishedWrap.setMargin(true);
		surveyFinishedWrap.setWidth("800px");
		surveyFinishedWrap.setStyleName("box-ok");
		
		Label surveyFinished = new Label();
		surveyFinishedWrap.addComponent(surveyFinished);

		panel2.addComponent(surveyFinishedWrap);
		
		Language l = controller.userSession.getLanguage();	
		Iterator it = controller.currentSurvey.getDomains().iterator();	
		while(it.hasNext()) {

			Domain d = (Domain)it.next();
			DomainText dt = controller.domainModel.getDomainText(d, l);
			
			int nQuestions = 0;
			Iterator<Question> questionIterator= d.getQuestions().iterator();
			while (questionIterator.hasNext()) {
				Question q = questionIterator.next();
				if (q.getSubquestions().size() > 0) {
					nQuestions = nQuestions + q.getSubquestions().size();
				}
				else {
					nQuestions++;
				}
			}
			
			int totalAnswers = controller.domainModel.getNumberOfAnswersForSurAndDomain(controller.getSurveyUserRole(), d);
			float progress = (float)totalAnswers/(float)nQuestions;
			if (progress != 1.0) {
				isSurveyFinished = false;
			}

			// container
			HorizontalLayout container = new HorizontalLayout();
			container.setSpacing(true);
			container.setMargin(true, false, false, false);
			
			//Title
			Label domainTitle = textHelper.span(dt.getText());
			domainTitle.setWidth("300px");
			
			//Indicator
			ProgressIndicator progressIndicator = new ProgressIndicator();
			progressIndicator.setValue(progress);
			progressIndicator.setWidth("");
			progressIndicator.setPollingInterval(180000);
			
			//Fill in Button
			Button actionButton = new Button(TM.get("editsurvey.fillout"), new Button.ClickListener() {
				public void buttonClick(ClickEvent event) {
					Domain domain = (Domain)event.getButton().getData();
					controller.getAppData().getLoaderLib().setUrl("FillOut/0/"+controller.getSurveyUserRole().getId()+"/"+domain.getId()+"/");
				}
			});
			actionButton.setData(d);
			actionButton.setIcon(Config.ICON_EDIT);
			
			if(controller.currentSurvey.getStage().getId() == Config.STAGE_EVALUATION) {
				actionButton.setEnabled(false);
			}
			
			container.addComponent(domainTitle);
			container.addComponent(progressIndicator);
			
			container.addComponent(actionButton);
			container.setComponentAlignment(actionButton, Alignment.MIDDLE_CENTER);
			
			container.setComponentAlignment(domainTitle, Alignment.MIDDLE_CENTER);
			container.setComponentAlignment(progressIndicator, Alignment.MIDDLE_CENTER);

			panel2.addComponent(container);
		}
		
		if (isSurveyFinished) {
			if(controller.currentSurvey.getStage().getId() == Config.STAGE_SURVEY) {
				surveyFinished.setValue(TM.get("editsurvey.tab1.surveyfinished"));
				surveyFinishedWrap.setVisible(true);
			} else {
				surveyFinished.setValue(TM.get("editsurvey.tab1.surveyfinished.results"));
				surveyFinishedWrap.setVisible(true);
			}
		}
	
		if(controller.getSurveyUserRole().getRole().getId() == Config.COORDINATOR_ID && controller.getSurveyUserRole().getFillOutRole() == null)
			panel2.setEnabled(false);
		
		return panel2;
	}

@Override
public void reBuild() {
	// TODO Auto-generated method stub
	
}

}
